//
//  LeftMenuViewController.m
//  DatingU
//
//  Created by linsyorozuya on 2017/9/6.
//  Copyright © 2017年 linsyorozuya. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "UIViewController+LeftSlide.h"
#import "UserModel.h"

static NSString *CellIdentifier = @"LeftMenuCell";
@interface LeftMenuViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSArray *tableViewItems;
@property (nonatomic, strong) UserModel *userModel;
@end

@implementation LeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(0, 0, 0, 0.6);
    // Do any additional setup after loading the view.
    _tableViewItems = @[@{@"imageName":@"",@"itemName":@""},
                        @{@"imageName":@"gerenxinx",@"itemName":@"工厂信息"},
                        @{@"imageName":@"qianb",@"itemName":@"我的钱包"},
                        @{@"imageName":@"chanx",@"itemName":@"我的产线"},
                        @{@"imageName":@"wodegongzuo",@"itemName":@"我的项目"},
                        @{@"imageName":@"yijianfankui-",@"itemName":@"意见反馈"}
                        ];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.delegate = self;
    [self.view addGestureRecognizer:tap1];
    UIView *whiteview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.6, SCREEN_HEIGHT)];
    whiteview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteview];
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.6, SCREEN_HEIGHT-40*displaywidth-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.bounces = NO;
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [whiteview addSubview:_tableview];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    UIView *heardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.6, STATUSBAR_HEIGHT)];
    heardView.backgroundColor = ThemeColor;
    _tableview.tableHeaderView = heardView;
    UIButton *setUpBtn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth, SCREEN_HEIGHT-SCREER_HeiX-35*displaywidth, 25*displaywidth, 25*displaywidth) ImageName:@"shezhi" Target:self Action:@selector(setUpAction) Title:nil Font:12.0];
    setUpBtn.backgroundColor = [UIColor clearColor];
    [whiteview addSubview:setUpBtn];
    UIButton *frendBtn = [ZCControl createButtonWithFrame:CGRectMake(80*displaywidth, SCREEN_HEIGHT-SCREER_HeiX-35*displaywidth, 25*displaywidth, 25*displaywidth) ImageName:@"guanyuwomen" Target:self Action:@selector(frendAction) Title:nil Font:12.0];
    frendBtn.backgroundColor = [UIColor clearColor];
    [whiteview addSubview:frendBtn];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CFShow(CFBridgingRetain(infoDictionary));
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    UILabel *versionLabel = [ZCControl createLabelWithFrame:CGRectMake(105*displaywidth, SCREEN_HEIGHT-SCREER_HeiX-35*displaywidth, SCREEN_WIDTH*0.6-105*displaywidth, 25*displaywidth) Font:10 Text:[NSString stringWithFormat:@"版本: %@", app_Version] Color:RGB(150, 150, 150)];
    versionLabel.backgroundColor = [UIColor clearColor];
    versionLabel.textAlignment = NSTextAlignmentCenter;
    [whiteview addSubview:versionLabel];
    // 添加从左划入的功能
    [self initSlideFoundation];
}

//设置
- (void)setUpAction
{
    if (self.clectedDelected != nil)
    {
        [self.clectedDelected clickTypeValue:2];
    }
    [self hide];
}

//关于我们
- (void)frendAction
{
    if (self.clectedDelected != nil)
    {
        [self.clectedDelected clickTypeValue:3];
    }
    [self hide];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:self.tableview]) {
        return NO;
    }
    if ([touch.view isDescendantOfView:self.tableview.superview])
    {
        return NO;
    }
    return YES;
}

#pragma mark -- show or hide
- (void)showFromLeft
{
    [self show];
    [self requestGetUserInfo];
}

-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    [self hide];
}

#pragma mark -- TableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableViewItems.count;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.tableViewItems[indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row > 0)
    {
        cell.imageView.image = [UIImage imageNamed:dic[@"imageName"]];
        cell.textLabel.text = dic[@"itemName"];
        cell.textLabel.textColor = RGB(77, 77, 77);
        cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
    }
    else
    {
        CGFloat W = SCREEN_WIDTH*0.6;
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.6, 190*displaywidth)];
        backView.backgroundColor = ThemeColor;
        [cell.contentView addSubview:backView];
        cell.backgroundColor = [UIColor whiteColor];
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(20*displaywidth, 10*displaywidth, W-30*displaywidth, 20*displaywidth) Font:18.0 Text:@"" Color:[UIColor whiteColor]];
        [backView addSubview:nameLab];
        UIImageView *faceView = [ZCControl createImageViewWithFrame:CGRectMake(20*displaywidth, 50*displaywidth, 90*displaywidth, 90*displaywidth) ImageName:@"zhanwei"];
        faceView.layer.cornerRadius = 45*displaywidth;
        faceView.layer.masksToBounds = YES;
        faceView.contentMode = UIViewContentModeScaleAspectFill;
        faceView.backgroundColor = [UIColor whiteColor];
        [backView addSubview:faceView];
        if (![LTools storageForKey:SERVER_IsLogin])
        {
            nameLab.text = @"立即登录";
            UIButton *loginBtn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth, 10*displaywidth, W-30*displaywidth, 20*displaywidth) ImageName:nil Target:self Action:@selector(loginAction) Title:nil Font:12.0];
            loginBtn.backgroundColor = [UIColor clearColor];
            [backView addSubview:loginBtn];
            
        }
        else
        {
            if (_userModel)
            {
                nameLab.text = _userModel.name;
                [LTools cache:_userModel.headImg ForKey:@"loginHeadImg"];
                [faceView sd_setImageWithURL:[NSURL URLWithString:_userModel.headImg] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
                if ([_userModel.identityId integerValue] == 0 || [_userModel.identityId integerValue] == -1)
                {
                    UIButton *renzhengBtn = [ZCControl createButtonWithFrame:CGRectMake(120*displaywidth, 82.5*displaywidth, 70*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(renzhengAction) Title:@"" Font:14.0];
                    renzhengBtn.backgroundColor = [UIColor whiteColor];
                    renzhengBtn.layer.cornerRadius = 3;
                    renzhengBtn.layer.masksToBounds = YES;
                    [renzhengBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
                    [cell.contentView addSubview:renzhengBtn];
                    
                    if ([_userModel.identityId integerValue] == 0)
                    {
                        [renzhengBtn setTitle:@"立即认证" forState:UIControlStateNormal];
                        renzhengBtn.enabled = YES;
                    }
                    else
                    {
                        [renzhengBtn setTitle:@"认证中" forState:UIControlStateNormal];
                        renzhengBtn.enabled = NO;
                        
                    }
                } else {
                    UILabel *typeLabel = [ZCControl createLabelWithFrame:CGRectMake(20*displaywidth, 150*displaywidth, 30*displaywidth, 15*displaywidth) Font:10.0 Text:@"工厂" Color:[UIColor whiteColor]];
                    typeLabel.textAlignment = NSTextAlignmentCenter;
                    typeLabel.backgroundColor = RGB(136, 180, 252);
                    typeLabel.layer.cornerRadius = 2;
                    typeLabel.layer.masksToBounds = YES;
                    [cell.contentView addSubview:typeLabel];
                }
                
//                UIView *gardeView = [[UIView alloc] initWithFrame:CGRectMake(60*displaywidth, 150*displaywidth, 40*displaywidth, 15*displaywidth)];
//                gardeView.backgroundColor = RGB(231, 80, 76);
//                gardeView.layer.cornerRadius = 2;
//                gardeView.layer.masksToBounds = YES;
//                [cell.contentView addSubview:gardeView];
//                UIImageView *graderImgView = [ZCControl createImageViewWithFrame:CGRectMake(2.5, 0, 10*displaywidth, 15*displaywidth) ImageName:@"pingfen"];
//                graderImgView.contentMode = UIViewContentModeScaleAspectFit;
//                [gardeView addSubview:graderImgView];
//                UILabel *gardeLabel = [ZCControl createLabelWithFrame:CGRectMake(12.5*displaywidth, 0, 27.5*displaywidth, 15*displaywidth) Font:10.0 Text:_userModel.grade Color:[UIColor whiteColor]];
//                gardeLabel.textAlignment = NSTextAlignmentCenter;
//                [gardeView addSubview:gardeLabel];
            }
        }
        
        
    }
    return cell;
}

//个人资料
- (void)requestGetUserInfo
{
    WEAKSELF;
    [HYBaseHandler requestGetUserInfoParmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             weakSelf.userModel = [UserModel mj_objectWithKeyValues:obj[@"data"]];
             [LTools cache:weakSelf.userModel.phone ForKey:SERVER_LoginPhone];
         }
         else
         {
             //             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
         dispatch_async(dispatch_get_main_queue(), ^{
             [weakSelf.tableview reloadData];
         });
     } failed:^(NSError *error) {
         
     }];
}

//认证
- (void)renzhengAction
{
    if (self.clectedDelected != nil)
    {
        [self.clectedDelected clickTypeValue:1];
    }
    [self hide];
}

- (void)loginAction
{
    if (self.clectedDelected != nil)
    {
        [self.clectedDelected clickTypeValue:0];
    }
    [self hide];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.clectedDelected != nil)
    {
        [self.clectedDelected selectedValue:indexPath.row];
    }
    [self hide];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 200*displaywidth;
    }
    else
    {
        return 50*displaywidth;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
